﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using System.Linq;
using System.Configuration;
using System.Threading;
using System.Diagnostics;
using System.Globalization;
using System.Drawing.Drawing2D;
namespace ChatRobot
{
    public partial class ChatFrm : Form
    {
        const string key = "53020ce7aacb4d0a82926cb9e99f3f6b";
        HttpWebResponse Response = null;
        public ChatFrm()
        {
            InitializeComponent();
            richTextBox1.ReadOnly = true;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("发送内容不能为空");
                return;
            }
            string Contert = textBox1.Text;
            Contert.Trim();
            richTextBox1.Text += "YOU:" + Contert + "\n";
            Encoding encoding = Encoding.GetEncoding("utf-8");
            string url = "http://www.tuling123.com/openapi/api?key=" + key + "&info=" + Contert;
            string data = HttpGet(url, encoding);
            JsonReader reader = new JsonTextReader(new StringReader(data));
            while (reader.Read())
            {
                if(reader.TokenType==JsonToken.PropertyName&&reader.ValueType==Type.GetType("System.String")
                    && Convert.ToString(reader.Value) == "code")
                {
                    reader.Read();
                    switch (reader.Value.ToString())
                    {
                        case "100000":
                            reader.Read();
                            reader.Read();
                            richTextBox1.AppendText("小白：" + reader.Value + "\n");
                            break;
                        case "200000":
                            reader.Read();
                            reader.Read();
                            richTextBox1.AppendText("小白：" + reader.Value + "\n");
                            break;
                        default:
                            break;

                    }
                }
                if (Convert.ToString(reader.Value) == "url")
                {
                    reader.Read();
                    richTextBox1.AppendText("小白" + reader.Value + "\n");
                }
            }
            textBox1.Text = "";
            textBox1.Focus();

        }
        public static string HttpGet(string url,Encoding encoding)
        {
            string data = "";
            try
            {
                WebRequest request;   //使用url发出请求的类
                request = WebRequest.Create(url);
                request.Credentials = CredentialCache.DefaultCredentials;
                request.Timeout = 20000;
                WebResponse response;  //提供响应的类
                response = request.GetResponse();
                data=new StreamReader(response.GetResponseStream(),encoding).ReadToEnd();
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return data;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox1.Text = "";
            }
        }
    }
}
